package net.tribal.comic.entities

import java.util.Date;

class Page {

	//	Persistence Attributes
	String 	title
	String 	description
	String 	pathFile
	String 	nameFile
	Integer numberPage
	Comic 	comic
	
	
	boolean rateable 	= true
	boolean commentable = true
	boolean disabled 	= false

	Date 	dateCreated
	Date 	lastUpdated
	

	//	Not Persistence Attributes
	static transients = ['rate', 'pathFile']

	
	//	DB Relations
	static belongsTo = 	[createdBy:		User]
	
	static hasMany =	[alterPages:	AlterPage,
	                	 ratings:		Rate]
	

	//	DB Constraits
	static 	constraints = {
		title(blank:false, unique:true, maxSize:50)
		description(nullable:true, maxSize:1000)
		numberPage(nullable:true)
		comic(nullable:true)
	}
	

	//	Other Methods
	String getRate() {
		//	We calculated the average scores
		Float p = 0
		if(ratings){
			ratings.each(){ p += it.rate }
			p = p / ratings.size()
		}
		return p
	}
	
	String getPathFile() {
		def p = ""
		if (nameFile == null || nameFile == "") {
			nameFile = "noneComic.png"
			return "../images/"
		}
		return "../uploads/comic/"
	}
}